#!/usr/bin/env sh

STAGE3_URL=https://mirrors.aliyun.com/gentoo/releases/amd64/autobuilds/current-install-amd64-minimal/stage3-amd64-openrc-20210912T170541Z.tar.xz

echo "::format disk"
cfdisk /dev/sda

mkfs.ext2 /dev/sda1
mkswap /dev/sda2
mkfs.ext4 /dev/sda3

echo "::mount /root:"
mkdir -p /mnt/gentoo
mount /dev/sda3 /mnt/gentoo
swapon /dev/sda2

echo "::set date:"
ntpdate ntp.aliyun.com

echo "::install stage3:"
cd /mnt/gentoo/
wget ${STAGE3_URL}
tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner

echo "::select china mirrors:"
mirrorselect -i -o >> /mnt/gentoo/etc/portage/make.conf

echo "::config portage:"
mkdir --parents /mnt/gentoo/etc/portage/repos.conf
cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
cp --dereference /etc/resolv.conf /mnt/gentoo/etc/

echo 'MAKEOPTS="-j2"' >> /mnt/gentoo/etc/portage/make.conf
echo 'FEATURES="buildpkg getbinpkg"' >> /mnt/gentoo/etc/portage/make.conf
echo '#PORTAGE_BINHOST="http://binhost.com/packages"' >> /mnt/gentoo/etc/portage/make.conf
echo 'ACCEPT_LICENSE="*"' >> /mnt/gentoo/etc/portage/make.conf
echo 'I10N="en-US zh-CN en zh"' >> /mnt/gentoo/etc/portage/make.conf
echo 'LINGUAS="en-US zh-CN en zh"' >> /mnt/gentoo/etc/portage/make.conf

echo "::chroot to new root:"
mount --types proc /proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev 

chroot /mnt/gentoo /bin/bash
source /etc/profile
export PS1="(chroot) ${PS1}"

echo "::sync portage:"
emerge-webrsync
emerge --sync

echo "::config \$USE:"
nano -w /etc/portage/make.conf
emerge --info | grep ^USE

echo "::config \$ACCEPT_LICENSE:"

echo "set timezone:"
echo "Asia/Shanghai" > /etc/timezone
emerge --config sys-libs/timezone-data

locale -a
echo "::config locale:"
nano -w /etc/locale.gen
locale-gen

eselect locale list
echo "::eselect locale set n"

echo "::reload env:"
env-update && source /etc/profile && export PS1="(chroot) ${PS1}"


echo "::kernel install:"
# echo "::install kernel souces:"
# emerge --ask sys-kernel/gentoo-sources
# ls -l /usr/src/linux
# emerge --ask sys-apps/pciutils

# echo "::gen initramfs:"
# emerge --ask sys-kernel/genkernel
# genkernel --install initramfs
# genkernel --lvm --mdadm --install initram
# ls /boot/initramfs*

# genkernel all

# emerge --ask sys-kernel/gentoo-kernel
emerge --ask sys-kernel/gentoo-kernel-bin
emerge --depclean

nano -w /mnt/gentoo/etc/fstab
blkid
fdisk -l
sleep 3
ls /boot/kernel* /boot/initramfs*

# echo "::rebuild initramfs:"
# emerge --ask @module-rebuild
# emerge --config sys-kernel/gentoo-kernel
# emerge --config sys-kernel/gentoo-kernel-bin

echo "::install firmware:"
emerge --ask sys-kernel/linux-firmware

echo "::hostname:${hostname}"
nano -w /etc/conf.d/hostname

echo "::domain:\${dns_domain_lo}"
nano -w /etc/conf.d/net

echo "::config network:"
echo 'config_eth0="dhcp"' >> /etc/conf.d/net
cd /etc/init.d
ln -s net.lo net.eth0
rc-update add net.eth0 default

emerge --ask net-misc/dhcpcd
emerge --ask net-dialup/ppp
emerge --ask net-wireless/iw net-wireless/wpa_supplicant

echo "::install log tool:"
emerge --ask app-admin/sysklogd
rc-update add sysklogd default

emerge --ask sys-process/cronie
rc-update add cronie default

emerge --ask sudo layman

echo "::set root passwd:"
passwd

echo "::add user:"
useradd -m -G users,wheel,audio -s /bin/bash damon
passwd damon

echo "::install grub2:"
emerge --ask --verbose sys-boot/grub:2
grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

echo "::reboot:"
exit
cd /
umount -l /mnt/gentoo/dev{/shm,/pts,}
umount -R /mnt/gentoo 
